﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestCommonWindow.CustomControl
{
    /// <summary>
    /// RecordProcessMsg.xaml 的交互逻辑
    /// </summary>
    public partial class RecordProcessMsg : UserControl
    {
        public RecordProcessMsg()
        {
            InitializeComponent();
        }
        static Brush _Foreground = Brushes.Black;
        static double _FontSizeValue = 12;
        
        public Brush FontColor
        {
            get { return (Brush)GetValue(FontColorProperty); }
            set { SetValue(FontColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FontColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FontColorProperty =
            DependencyProperty.Register("FontColor", typeof(Brush), typeof(RecordProcessMsg), new PropertyMetadata(Brushes.Black, new PropertyChangedCallback(OnAddFontColor)));

        private static void OnAddFontColor(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            _Foreground = (Brush)e.NewValue;
        }


        //依赖属性《用于绑定》          
        public string ProcessMsg
        {
            get { return (string)GetValue(ProcessMsgProperty); }
            set { SetValue(ProcessMsgProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ProcessMsg.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ProcessMsgProperty =
            DependencyProperty.Register("ProcessMsg", typeof(string), typeof(RecordProcessMsg), new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnAddProcessMsg)));

        //依赖属性改变事件

        private static void OnAddProcessMsg(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //MessageBox.Show(string.Format("进入函数{0}",nameof(OnAddProcessMsg)));
            if (d != null)
            {
                if (e.NewValue != null)
                {
                    FlowDocument doc = new FlowDocument();
                    doc.LineHeight = 0.1;
                    Paragraph p = new Paragraph();
                    Run r = new Run((string)e.NewValue);
                    r.Foreground = _Foreground;
                    r.FontSize = _FontSizeValue;
                    p.Inlines.Add(r);//Run级元素添加到Paragraph元素的Inlinedoc.Blocks.Add(p);//Paragraph级元素添加到流文档的块级元素
                    ((RecordProcessMsg)d).rtbProcess.Document.Blocks.Add(p);
                    ((RecordProcessMsg)d).rtbProcess.ScrollToEnd(); //滚动到最后一行
                }
            }
        }

                            
        public bool IsClear
        {
            get { return (bool)GetValue(IsClearProperty); }
            set { SetValue(IsClearProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsClear.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsClearProperty =
            DependencyProperty.Register("IsClear", typeof(bool), typeof(RecordProcessMsg), new PropertyMetadata(false,propertyChangedCallback:new PropertyChangedCallback(OnAddIsClear)));
        private static void OnAddIsClear(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //确定清除内容
            if (Convert.ToBoolean(e.NewValue))
            {
                ((RecordProcessMsg)d).rtbProcess.Document.Blocks.Clear();
            }
        }


        //字体大小
        public int FontSizeValue
        {
            get { return (int)GetValue(FontSizeValueProperty); }
            set { SetValue(FontSizeValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FontSizeValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FontSizeValueProperty =
            DependencyProperty.Register("FontSizeValue", typeof(int), typeof(RecordProcessMsg), new PropertyMetadata(12,OnAddFontSizeValue));

        private static void OnAddFontSizeValue(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
             int value = (int)e.NewValue;
            _FontSizeValue = (double)value;
        }
    }
}
